﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ChgPwd;
using System.Web.Security;

/**
 * @version 1.0
 * @authors: 
 *      Fabien DUCHENE f.duchene@car-online.fr 
 *      Ludovic FANTON www.fanton.l@gmail.com  
 */

public partial class _Default : System.Web.UI.Page
{
    protected String UserName;
    /* 
     * @requirements:
     *   - in order to load the web application, the user has to be authenticated against the Active Directory identity referential
     */ 
    protected void Page_Load(object sender, EventArgs e)
    {
        // retrieval of the user logon name
        UserName = Request.ServerVariables.Get("LOGON_USER");

        // display it, and do not ask the user to manually enter it
        UserMessage.Text = UserName;

        UserMessage.Text = Language.PWD_COMPLEXITY_POLICY_CONTENT;
    }


    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
        /* basic checks */
            // check if the user entered the same password twice, otherwise issue an error
            if (NewPassword1.Text.CompareTo(NewPassword2.Text) != 0) 
              throw new Exception(Language.PWD_DO_NOT_MATCH);
            
            // check if the user-provided old password and the new one are the same. if it is the case, then raise an exception.
            if (NewPassword1.Text.CompareTo(OldPassword.Text) == 0)
                throw new Exception(Language.NEW_PWD_IS_SAME_AS_OLD_ONE);


            // bind a new AD LDAP connection using the provided credentials
                /* if any connection error occurs, an error is automatically thrown */
            ADUser user = new ADUser(UserName,OldPassword.Text);

            // then try to actually change the password. 
                throw new Exception(!user.ChangePassword(NewPassword1.Text)
                    ?Language.PWD_WASNT_CHANGED
                    :Language.PWD_CHANGED
                );
        }

        // in case of any exception the inner message content is displayed
        catch (Exception ee)
        {
            UserMessage.Text = ee.Message;
        }
    }
}